import { findAllCategory } from '@/api/category'
import { topCategory } from '@/api/constans'
export default {
  namespaced: true,
  state: {
    list: topCategory.map(item => ({ name: item }))
  },
  mutations: {
    setList (state, payload) {
      state.list = payload
    },
    // 控制分类的显示
    show (state, id) {
      const category = state.list.find(item => item.id === id)
      category.open = true
    },
    // 控制分类的隐藏
    hide (state, id) {
      const category = state.list.find(item => item.id === id)
      category.open = false
    }
  },
  actions: {
    async getList ({ commit }) {
      const { result } = await findAllCategory()
      // 给分类加一个open属性
      result.forEach(item => { item.open = false })
      commit('setList', result)
    }
  }
}
